from xml.dom.minidom import *
import tensorflow as tf
import cv2
import json
import urllib
def xmlWrite(filename,jsonxml,xmlpath,xpath):
    doc = Document()
    #构造数据的根节点
    annotation = doc.createElement("annotation")
    doc.appendChild(annotation)
    folder = doc.createElement('folder')
    filenames= doc.createElement('filename')
    annotation.appendChild(filenames)
    filename=str(filename)
    filenamelabel = doc.createTextNode(filename[filename.rfind('/')+1:])
    filenames.appendChild(filenamelabel)
    annotation.appendChild(folder)
    folderName = doc.createTextNode('imagename')
    folder.appendChild(folderName)
    path = doc.createElement('path')
    pathurl = doc.createTextNode(filename)
    annotation.appendChild(path)
    path.appendChild(pathurl)
    source = doc.createElement('source')
    annotation.appendChild(source)
    database = doc.createElement('database')
    source.appendChild(database)
    known = doc.createTextNode('Unknown')
    database.appendChild(known)
    size = doc.createElement('size')
    annotation.appendChild(size)
    img = cv2.imread(filename)
    cires = img.shape
    width = doc.createElement('width')
    widthdistence= doc.createTextNode(str(cires[1]))
    width.appendChild(widthdistence)
    size.appendChild(width)
    height = doc.createElement('height')
    heightDistance= doc.createTextNode(str(cires[0]))
    size.appendChild(height)
    height.appendChild(heightDistance)
    depth = doc.createElement('depth')
    depthDistence= doc.createTextNode(str(cires[2]))
    size.appendChild(depth)
    depth.appendChild(depthDistence)
    segmented = doc.createElement('segmented')
    se= doc.createTextNode('0')
    segmented.appendChild(se)
    annotation.appendChild(segmented)
    s = json.loads(jsonxml)
    labels=s['objs']
    for item in labels:
        obj = doc.createElement('object')
        annotation.appendChild(obj)
        name = doc.createElement('name')
        label = doc.createTextNode(item['label'])
        name.appendChild(label)
        obj.appendChild(name)
        pose = doc.createElement('pose')
        pose.appendChild(doc.createTextNode('Unspecified'))
        obj.appendChild(pose)
        truncated = doc.createElement('truncated')
        truncated.appendChild(doc.createTextNode('1'))
        obj.appendChild(truncated)
        difficult = doc.createElement('difficult')
        difficult.appendChild(doc.createTextNode('0'))
        obj.appendChild(difficult)
        bndbox= doc.createElement('bndbox')
        obj.appendChild(bndbox)
        xmin= doc.createElement('xmin')
        bndbox.appendChild(xmin)
        xminlabel=doc.createTextNode(str(item['xmin']))
        xmin.appendChild(xminlabel)
        ymin=doc.createElement('ymin')
        bndbox.appendChild(ymin)
        yminlabel=doc.createTextNode(str(item['ymin']))
        ymin.appendChild(yminlabel)
        xmax=doc.createElement('xmax')
        bndbox.appendChild(xmax)
        xmaxlabel= doc.createTextNode(str(item['xmax']))
        xmax.appendChild(xmaxlabel)
        ymax=doc.createElement('ymax')
        bndbox.appendChild(ymax)
        ymaxlabel= doc.createTextNode(str(item['ymax']))
        ymax.appendChild(ymaxlabel)
    if not tf.gfile.Exists(xpath):
        tf.gfile.MakeDirs(xpath)
    f = open(xmlpath, 'w')
    f.write(doc.toprettyxml(indent=''))
    f.close()

def convertimgset(rootdir,list,img_sets):
    #拿到图片的索引,以及标注的图片信息
    for item in list:
       imgindex, imgJson, imgUrl= item['index'], item['imgJson'], item['imgUrl']
       filename = imgindex
       ls = "{}/JPEGImages".format(rootdir)
       if not tf.gfile.Exists(ls):
                tf.gfile.MakeDirs(ls)
       urllib.request.urlretrieve(str(imgUrl), "{}/JPEGImages/{}.jpg".format(rootdir,filename))
       #图像xml的生成
       xmlWrite("{}/JPEGImages/{}.jpg".format(rootdir,filename),
                imgJson,
                "{}/Annotations/{}.xml".format(rootdir,filename),
                "{}/Annotations".format(rootdir))
       if not tf.gfile.Exists("{}/ImageSets/Main".format(rootdir)):
            tf.gfile.MakeDirs("{}/ImageSets/Main".format(rootdir))
        #图像生成txt
       ImageSets = "{}/ImageSets/Main/{}.txt".format(rootdir, img_sets)
       f = open(ImageSets, 'w')
       print(filename)
       f.write("{}".format(filename)+"\n")
    f.close()
















































